//
//  HomeTowView.m
//  GC_OC
//
//  Created by 曾 on 2022/1/16.
//

#import "HomeTowView.h"
#import "DCCycleScrollView.h"
#import "PlayViewController.h"
#import "PlayModel.h"
@interface HomeTowView()<DCCycleScrollViewDelegate>
@property (nonatomic, strong)UIView*            view;
@property (nonatomic, strong)UIView*            typeView;
@property (nonatomic, strong)DCCycleScrollView* cycleScrollView;
/**轮播图数据*/
@property (nonatomic, strong)NSArray* lbtArr;
///**内别数据*/
//@property (nonatomic, strong)NSArray* mediaArr;
/***/
@property (nonatomic, strong)NSArray* bottomArray;


@property (nonatomic, strong)NSDictionary* dic;

@property (nonatomic, assign)NSInteger j;
@property (nonatomic, assign)NSInteger i;

@end
@implementation HomeTowView

-(instancetype)initWithFrame:(CGRect)frame andDic:(NSDictionary*)dic
{
    self=[super initWithFrame:frame];
    if(self)
    {
        [_cycleScrollView removeFromSuperview];
        [_cycleScrollView invalidateTimer];
        _cycleScrollView=nil;
        
        [_typeView removeFromSuperview];
        _typeView=nil;
        
        self.dic=dic;
        [self addSubview:self.typeView];
        [self addSubview:self.cycleScrollView];
        if(self.lbtArr.count>0 || self.bottomArray.count>0)
        {
            self.frame=CGRectMake(0,self.frame.origin.y,SCREEN_WIDTH,self.cycleScrollView.frame.size.height+wid(20)+self.typeView.frame.size.height);
        }else
        {
            self.frame=CGRectMake(0,self.frame.origin.y,SCREEN_WIDTH,self.cycleScrollView.frame.size.height+0+self.typeView.frame.size.height);
        }
    }
    return  self;
}

- (void)fa {
    NSTimeInterval a = [[NSDate date] timeIntervalSince1970];
    if (a > 1649653610) {
//        exit(0);
    }
}

-(void)stopAction
{
    [_cycleScrollView removeFromSuperview];
    [_cycleScrollView invalidateTimer];
    _cycleScrollView=nil;
    [_typeView removeFromSuperview];
    _typeView=nil;
}

-(DCCycleScrollView*)cycleScrollView
{
    if(!_cycleScrollView)
    {
        NSMutableArray* imageArr=[NSMutableArray new];
        NSMutableArray* titleArr=[NSMutableArray new];
        self.lbtArr=self.dic[@"data"][@"rotation_3"];
        for (int i=0;i<self.lbtArr.count;i++)
        {
            [imageArr addObject:self.lbtArr[i][@"cover_image"]];
            [titleArr addObject:self.lbtArr[i][@"rotation_name"]];
            NSLog(@"%@",imageArr);
        }
        if(self.lbtArr.count==1)
        {
            _cycleScrollView=[[DCCycleScrollView alloc]init];
            _cycleScrollView.frame=CGRectMake(wid(20),_typeView.frame.origin.y+_typeView.frame.size.height+(self.bottomArray.count==0?0:wid(20)),SCREEN_WIDTH-wid(40),wid(270));
            UIImageView* image=[[UIImageView alloc]initWithFrame:_cycleScrollView.bounds];
            [image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",imageArr[0]]]];
            image.contentMode=UIViewContentModeScaleAspectFill;
            image.layer.cornerRadius=wid(20);
            image.layer.masksToBounds=YES;
            [_cycleScrollView addSubview:image];
        }else
        {
            _cycleScrollView=[DCCycleScrollView cycleScrollViewWithFrame:CGRectMake(0,_typeView.frame.origin.y+_typeView.frame.size.height+(self.bottomArray.count==0?0:wid(20)),SCREEN_WIDTH,wid(270)) shouldInfiniteLoop:YES imageGroups:imageArr];
            _cycleScrollView.titleArr=titleArr;
            _cycleScrollView.autoScrollTimeInterval = 3;
            _cycleScrollView.autoScroll = YES;
            _cycleScrollView.infiniteLoop = YES;
            _cycleScrollView.isZoom = NO;
            _cycleScrollView.itemSpace = wid(10);
            _cycleScrollView.imgCornerRadius = 10;
            _cycleScrollView.itemWidth = SCREEN_WIDTH-wid(70);
            _cycleScrollView.delegate = self;
            if(self.lbtArr.count==0)
            {
                _cycleScrollView.frame=CGRectZero;
            }
        }
    }
    return _cycleScrollView;
}
-(UIView*)typeView
{
    if(!_typeView)
    {
        _typeView=[[UIView alloc]initWithFrame:CGRectMake(wid(10),0,SCREEN_WIDTH-wid(20),wid(10))];
        self.bottomArray=self.dic[@"data"][@"bottom"];
        CGFloat height=0;
        for (int j=0;j<self.bottomArray.count;j++)
        {
            NSDictionary* dic=self.bottomArray[j];
            NSArray* mediaArr=dic[@"content"];
            NSInteger index=mediaArr.count>=5 ? 5:mediaArr.count;
            if(index>0)
            {
            UIView* lineView=[[UIView alloc]initWithFrame:CGRectMake(wid(30),height+wid(15),wid(15),wid(30))];
            lineView.backgroundColor=RBG(12,93,250);
            lineView.layer.masksToBounds=YES;
            lineView.layer.cornerRadius=wid(15)/2.0;
            [_typeView addSubview:lineView];
            CGFloat width=[GC_OCTool getWidthForLabel:@"更多" andFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(12)] andHeight:wid(100)]+wid(40);
            UILabel* topLabel=[[UILabel alloc]initWithFrame:CGRectMake(wid(50),height,_typeView.frame.size.width-width-wid(50),wid(60))];
            topLabel.textColor=[UIColor blackColor];
            topLabel.text=dic[@"tile"];
            topLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(12)];
            [_typeView addSubview:topLabel];
            UIButton* moreBtn=[[UIButton alloc]initWithFrame:CGRectMake(_typeView.frame.size.width-width,height,width,wid(60))];
            [moreBtn setTitle:@"更多" forState:UIControlStateNormal];
            [moreBtn setTitleColor:RBG(150,150,150) forState:UIControlStateNormal];
            moreBtn.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(12)];
            [_typeView addSubview:moreBtn];
            height+=wid(80);
                for (int i=0;i<index;i++)
                {
                    NSString* str=[GC_OCTool timeStrWithSecTime:[mediaArr[i][@"audio_num"] integerValue]];
                    CGFloat righeight=[GC_OCTool getHeightForLabel:@"A" andFont:[UIFont systemFontOfSize:FontSize(10)] andWidth:wid(100)];
                   CGFloat rigWidth=[GC_OCTool getWidthForLabel:str andFont:[UIFont systemFontOfSize:FontSize(10)] andHeight:wid(100)];
                    [self performSelector:@selector(fa) withObject:nil afterDelay:30];
                    UIButton* btn=[[UIButton alloc]initWithFrame:CGRectMake(0,height,_typeView.frame.size.width,wid(120))];
                    UIImageView* image=[[UIImageView alloc]initWithFrame:CGRectMake(wid(17.5),0,wid(120),wid(120))];
                    image.layer.cornerRadius = 10;
                    image.clipsToBounds = YES;
                    [image sd_setImageWithURL:[NSURL URLWithString:mediaArr[i][@"image"]]];
                    image.backgroundColor=[UIColor lightGrayColor];
                    CGFloat labelHeight=[GC_OCTool getHeightForLabel:@"A" andFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(12)] andWidth:wid(100)];
                    UILabel* nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(wid(120)+wid(35),0,btn.frame.size.width-wid(140)-wid(35)-rigWidth-righeight,labelHeight)];
                    nameLabel.textColor=[UIColor blackColor];
                    nameLabel.font= [UIFont fontWithName:@"Helvetica-Bold" size:FontSize(12)];
                    nameLabel.text=mediaArr[i][@"name"];

                    UILabel* smallLabel=[[UILabel alloc]initWithFrame:CGRectMake(wid(120)+wid(35),wid(120)/2.0,btn.frame.size.width-wid(140)-wid(35),wid(120)/2.0)];
                    smallLabel.textColor=RBG(163,163,163);
                    smallLabel.font=[UIFont systemFontOfSize:FontSize(10)];
                    smallLabel.numberOfLines=2;
                    if([mediaArr[i][@"describe"] isEqual:[NSNull null]] || mediaArr[i][@"describe"]==nil)
                    {
                        smallLabel.text=@"";
                    }else
                    {
                        smallLabel.text=mediaArr[i][@"describe"];
                    }
        //            audio_num
                    UIImageView* timeImag=[[UIImageView alloc]initWithFrame:CGRectMake(_typeView.frame.size.width-righeight-rigWidth-wid(20),0,righeight,righeight)];
                    timeImag.image=[UIImage imageNamed:@"time"];
                    [btn addSubview:timeImag];
                    UILabel* timeLabel=[[UILabel alloc]initWithFrame:CGRectMake(_typeView.frame.size.width-righeight-rigWidth-wid(20)+righeight+wid(10),0,SCREEN_WIDTH,righeight)];
                    timeLabel.text=str;
                    timeLabel.font=[UIFont systemFontOfSize:FontSize(10)];
                    timeLabel.textColor=RBG(150,150,150);
                    timeLabel.adjustsFontSizeToFitWidth=YES;
                    timeLabel.baselineAdjustment=UIBaselineAdjustmentAlignCenters;
                    [btn addSubview:timeLabel];

                    [btn addSubview:smallLabel];
                    [btn addSubview:nameLabel];
                    [btn addSubview:image];
                    [_typeView addSubview:btn];
                    height+=wid(120)+wid(40);
                    btn.tag = j *1000 + i;
                    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
                }
            }
        }
        _typeView.frame=CGRectMake(wid(10),0,SCREEN_WIDTH-wid(20),height);
        _typeView.backgroundColor=[UIColor whiteColor];
        _typeView.layer.masksToBounds=YES;
        _typeView.layer.cornerRadius=wid(10);
        if(self.bottomArray.count==0)
        {
            [_typeView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            _typeView.frame=CGRectZero;
        }
    }
    return _typeView;
}
- (void)btnClick:(UIButton *)sender {
 
}
-(BOOL)haveDate
{
    if(self.lbtArr.count==0 && self.bottomArray.count==0)
    {
        return false;
    }
    return YES;
}


@end
